package user

import (
	"server/appControl/sys"
	"server/data/db"
	"server/public"
	"time"
)

/*
*  -*- coding:utf8 -*-
*
*  用户相关操作封装
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/16 下午6:14
*  @File    : userOp.go
 */

const (
	StsLoginTrue         = 1 // 登录成功
	StsLoginFail         = 2 // 登录失败
	StsRegisterTrue      = 3 // 注册成功
	StsRegisterNameFail  = 4 // 注册失败, 用户名被占用
	StsRegisterEmailFail = 5 // 邮箱已经被注册
	StsLoginOut          = 6 // 退出
	StsOffLine           = 7 // 离线
	StsUnusual           = 8 // 用户状态异常
)

type UserInfo struct {
	UserId     int    `json:"userId"`
	UserName   string `json:"userName"`
	UserPasswd string `json:"userPasswd"`
	Email      string `json:"Email"`
	UserStatus int    `json:"userStatus"`
	FriendCnt  int    `json:"FriendCnt"` // 好友数量
}

type UserOpSts struct {
	UserId     int    `json:"userId"`
	UserStatus int    `json:"userStatus"`
	Msg        string `json:"Msg"`
}

type FriendInfo struct {
	UserId     int    `json:"userId"`
	UserName   string `json:"userName"`
	Email      string `json:"Email"`
	UserStatus int    `json:"userStatus"`
}

type UserOption struct {
	Info UserInfo
}

func NewUserOp(name string) *UserOption {
	return &UserOption{
		Info: UserInfo{
			UserName: name,
		},
	}
}

func (u *UserOption) UserLogin() (sts int, err error) {
	public.Loggerf.Infof("Login: %v", u.Info)

	// 登录逻辑
	sts = StsLoginTrue
	u.CreateUserId()
	sql := "select uid, user_passwd, email, user_sts from user_info where user_name = ?"
	res, err := db.MysqlDb.Query(sql, u.Info.UserName)
	if err != nil {
		public.Loggerf.Warnf("Select err: %v", err)
	}
	//TODO: 添加邮箱登录

	var uid int
	var passwd string
	var email string
	var UsrSts int
	for res.Next() {
		if err = res.Scan(&uid, &passwd, &email, &UsrSts); err != nil {
			public.Loggerf.Errorf("Query user id failed: %v", err)
			sts = StsLoginFail
		}
	}

	if u.Info.UserPasswd != passwd {
		sts = StsLoginFail
		return
	}

	u.Info.UserId = uid
	u.Info.UserPasswd = passwd
	u.Info.Email = email
	u.Info.UserStatus = UsrSts

	return
}

func (u *UserOption) UserRegister() (sts int, err error) {
	public.Loggerf.Infof("register: %v", u.Info)

	// 产生Id
	u.CreateUserId()
	sts = StsRegisterTrue
	u.Info.UserStatus = sts

	// 查询用户是否存在
	sql := "select count(*) from user_info where user_name = ?"
	cnt, err := db.MysqlDb.QueryCount(sql, u.Info.UserName)
	public.Loggerf.Debugf("Name Count: %v", cnt)
	if err != nil {
		public.Loggerf.Warnf("Select user name err: %v", err)
	}
	if cnt != 0 {
		sts = StsRegisterNameFail
		return
	}

	// 插入数据库
	sql = "INSERT INTO user_info " +
		"(uid, user_name, user_passwd, email, user_sts, create_time)" +
		"values (?, ?, ?, ?, ?, ?)"
	res, err := db.MysqlDb.Insert(sql, u.Info.UserId, u.Info.UserName, u.Info.UserPasswd, u.Info.Email, u.Info.UserStatus, time.Now().Format("2006-01-02 15:04:05"))
	if err != nil {
		public.Loggerf.Warnf("Insert err: %v, %v", res, err)
		sts = StsRegisterEmailFail
	}

	return
}

//CreateUserId
func (u *UserOption) CreateUserId() {
	u.Info.UserId = sys.SysInfo.CurrentUserId
	sys.SysInfo.CurrentUserId++

	// 数据持久化操作
	go sys.UpdateCurrentUserId()

}
